package com.application.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.databinding.DataBindingUtil;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LifecycleOwner;

import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.application.BR;
import com.application.R;
import com.application.adapter.ViewPagerAdapter;
import com.application.base.BaseFragment;
import com.application.databinding.FragmentFirstBinding;
import com.application.viewmodel.FirstViewModel;
import com.google.android.material.navigation.NavigationView;
import com.google.android.material.tabs.TabLayoutMediator;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends BaseFragment<FragmentFirstBinding, FirstViewModel> {

    public FirstFragment() {
    }

    public static FirstFragment newInstance() {
        return new FirstFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initLayoutId() {
        return R.layout.fragment_first;
    }

    @Override
    protected int initVariableId() {
        return BR.firstViewModel;
    }

    @Override
    protected void initData() {
        super.initData();
        binding.setFragment(this);
        initViewPagerAdapter();
    }

    @Override
    protected void initViewObservable() {
        super.initViewObservable();
        initDrawerAndToolbar();
    }

    private void initViewPagerAdapter() {
        List<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(FirstHomeFragment.newInstance());
        fragmentList.add(FirstMusicFragment.newInstance());
        fragmentList.add(FirstMovieFragment.newInstance());

        List<String> titleList = viewModel.getTabTitles().getValue();

        ViewPagerAdapter adapter = new ViewPagerAdapter(this, fragmentList, titleList);
        binding.tabFirstViewpager.setAdapter(adapter);

        new TabLayoutMediator(binding.tabFirstLayout, binding.tabFirstViewpager,
                (tab, positon) -> tab.setText(titleList.get(positon))).attach();
    }

    private void initDrawerAndToolbar() {
        NavigationView navigationView = binding.navView;
        Toolbar toolbar = binding.toolbar;
        DrawerLayout drawerLayout = binding.drawerLayout;
        toolbar.setNavigationOnClickListener(v -> drawerLayout.openDrawer(navigationView));

        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
                String message = viewModel.handleNavigationItemClick(menuItem.getItemId());
                if (!message.isEmpty()) {
                    Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
                }
                drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });
    }

    public void onSomeButtonClick(View view) {

    }

}